package cn.wolfcode.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.domain.RolePermission;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRolePermissionService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author jishushen
 * @create 2021-02-19 14:50
 * 系统管理之角色管理
 */
@Api(value = "角色列表",description = "角色资源控制器")
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private IRolePermissionService rolePermissionService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"role:list","角色列表"},logical = Logical.OR)
    public String list(Model model, QueryObject qo){
        PageInfo<Role> pr = roleService.listPage(qo);
        model.addAttribute("pageInfo",pr);
        return "role/list";
    }

    @RequestMapping("/input.do")
    public String input(Long id,Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        if (id != null){
            //修改
            //根据id查出对应的角色
            Role role = roleService.selectByPrimaryKey(id);
            //再根据角色id根据多表查询查出对应的权限
            List<Permission> permissionList =permissionService.selectByRoleId(id);
            model.addAttribute("permissionList",permissionList);
            model.addAttribute("role",role);
        }
        return "role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] ids){
        if (role.getId() != null){
            //修改
            roleService.updateByPrimaryKey(role);
            //删除所有的对应的role_permission表中的数据
            rolePermissionService.deleteByRoleId(role.getId());
            //判断ids等不等于null,不等于null添加
            if(ids != null){
                for (Long id : ids) {
                    rolePermissionService.insert(new RolePermission(role.getId(),id));
                }
            }

        }else {
            //添加
            roleService.insert(role);
            Long id = role.getId();
            if (ids != null){
                for (Long aLong : ids) {
                    rolePermissionService.insert(new RolePermission(id,aLong));
                }
            }

        }
        return "redirect:/role/list.do";
    }

    @GetMapping("/delete")
    public String delete(Long id){
        roleService.deleteByPrimaryKey(id);
        rolePermissionService.deleteByRoleId(id);
        return "redirect:/role/list.do";
    }


}
